package com.ruoyi.common.utils;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.BaseException;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Month;
import java.util.*;

/**
 * 时间工具类
 * 
 * @author ruoyi
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
{
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static String YYYY年MM月DD日 = "yyyy年MM月dd日";

    public static String HH_MM = "HH:mm";
    
    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", 
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     * 
     * @return Date() 当前日期
     */
    public static Date getNowDate()
    {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     * 
     * @return String
     */
    public static String getDate()
    {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime()
    {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow()
    {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format)
    {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date)
    {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date)
    {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts)
    {
        try
        {
            return new SimpleDateFormat(format).parse(ts);
        }
        catch (ParseException e)
        {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }


    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str)
    {
        if (str == null)
        {
            return null;
        }
        try
        {
            return parseDate(str.toString(), parsePatterns);
        }
        catch (ParseException e)
        {
            return null;
        }
    }
    
    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate()
    {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate)
    {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    public static String getTimePoor(Long duration) {
        long nh = 60 * 60;
        long nm = 60;
        // 计算差多少小时
        long hour = duration / nh;
        // 计算差多少分钟
        long min = duration % nh / nm;
        // 计算差多少秒
        long sec = duration % nh % nm;
        return fixedLength(hour, 2) + ":" + fixedLength(min, 2) + ":" + fixedLength(sec, 2);
    }

    public static List<String> getFuture7DaysDate(){
        List<String> dateList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateList.add(dateFormat.format(calendar.getTime()));
        for (int i = 1; i <= 6; i++) {
            calendar.add(Calendar.DAY_OF_YEAR, 1);
            dateList.add(dateFormat.format(calendar.getTime()));
        }
        return dateList;
    }

    /**
     * @param value:  要转换的值
     * @param length: 字符串的长度
     * @return java.lang.String
     * @Description 返回大于指定长度的字符串 不足位首位补0
     * @author 18305
     * @Datetime 2022/1/26 9:36
     */
    public static String fixedLength(Object value, int length) {
        String formatStr = "%0" + length + "d";
        return String.format(formatStr, value);
    }

    /**
     * 获取 year 年第 week 周的开始日期和结束日期
     * @param year:	年
     * @param week:	第几周
     * @return: java.util.Map<java.lang.String, java.lang.String>
     * @author: kenn
     * @date: 2023/11/16 9:38
     */
    public static Map<String, LocalDate> weekOfYear(int year, int week) {
        Map<String, LocalDate> resultMap = new HashMap<>(2);
        LocalDate startDate;
        LocalDate endDate;
        LocalDate yearFirstDate = LocalDate.of(year, 1, 1);
        int dayOfWeek = yearFirstDate.getDayOfWeek().getValue();
        if (week == 1) {
            startDate = yearFirstDate;
            endDate = yearFirstDate.plusDays(Constants.WEEK_DAY - dayOfWeek);
        } else {
            startDate = yearFirstDate.plusDays((long) (week - 1) * Constants.WEEK_DAY - (dayOfWeek - 1));
            endDate = yearFirstDate.plusDays((long) (week - 1) * Constants.WEEK_DAY + (Constants.WEEK_DAY - dayOfWeek));
        }
        resultMap.put("startDate", startDate);
        resultMap.put("endDate", endDate);
        return resultMap;
    }

    public static int getNowDayQuarter(){
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 获取当前月份
        Month currentMonth = currentDate.getMonth();

        // 确定当前季度
        int currentQuarter = (currentMonth.getValue() - 1) / 3 + 1;
        return currentQuarter;
    }

    public static int getLastQuarter(){
        return getNowDayQuarter() - 1;
    }

    /**
     * 判断时间范围正确性
     * @param startDate: 开始时间
     * @param endDate: 结束时间
     * @return: void
     * @author: kenn
     * @date: 2023/12/8 10:16
     */
    public static void judgeDate(LocalDate startDate, LocalDate endDate) {
        if (endDate.isBefore(startDate)) {
            throw new BaseException("结束时间不能小于开始时间");
        }
    }

    /**
     * 获取时间周期包含的天数
     * @param startDate: 开始时间
     * @param endDate: 结束时间
     * @return: int
     * @author: kenn
     * @date: 2023/12/8 10:17
     */
    public static int dayLength(LocalDate startDate, LocalDate endDate) {
        judgeDate(startDate, endDate);
        int day = 0;
        while (!startDate.isAfter(endDate)) {
            day++;
            startDate = startDate.plusDays(1);
        }
        return day;
    }

    /**
     * 获取24小时时间段
     * @return
     */
    public static List<String> generateTimeSlots() {
        List<String> slots = new ArrayList<>();
        for (int hour = 0; hour < 24; hour++) {
            slots.add(String.format("%02d:00", hour));
        }
        return slots;
    }

}
